package com.zenithsun.news.solr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.syzg.base.aduit.config.AuditConfig;
import com.syzg.base.aduit.model.Audit;
import com.syzg.base.aduit.service.AuditService;
import com.syzg.common.util.SearchMap;
import com.syzg.solr.SolrManage;
import com.zenithsun.common.bean.OutMessage;
import com.zenithsun.common.bean.OutMessageState;
import com.zenithsun.news.newsinfo.model.Newsinfo;
import com.zenithsun.news.newsinfo.service.NewsinfoService;
import com.zenithsun.news.solr.util.SolrHelper;

@Controller
@RequestMapping("/solr")
public class SolrManagerController {
	@Resource
	private AuditService auditService;
	@Resource
	private NewsinfoService infoService ;
	@RequestMapping("/main")
	public ModelAndView main() throws Exception {
		ModelAndView mv = new ModelAndView("/zenithsun/news/solr/main.jsp");
		long solrCount = SolrHelper.getInstance().getCount("*");
		SearchMap map = SearchMap.forClass(Newsinfo.class);
		map.eq("isDelete", (short)0);
		long infoCount = infoService.getCount(map);
		mv.addObject("solrCount",solrCount);
		mv.addObject("infoCount",infoCount);
		return mv;
		
	}
	
	/**
	 * 全量导入
	 * @return
	 */
	@RequestMapping(value="/data/fullImport", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String dataFullImport(HttpServletRequest request) throws Exception{
		OutMessage out = new OutMessage();
		Audit audit = new Audit("solr数据全量导入", AuditConfig.DATA_FULL_IMPORT, request);
		try {
			SolrManage.dataFullImport();
			audit.setFlag(AuditConfig.OP_SUCCESS);
			out.setState(OutMessageState.Success);
			out.setContent("全量导入成功");
		} catch (Exception e) {
			// TODO: handle exception
			out.setState(OutMessageState.Fail);
			out.setContent("全量导入失败");
			audit.setFlag(AuditConfig.OP_FAILURE);
		}
		auditService.saveAuditLog(audit);
		System.out.println(JSON.toJSONString(out));
		return JSON.toJSONString(out);
	}
	/**
	 * 增量导入
	 * @return
	 */
	@RequestMapping(value="/data/deltaImport", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String dataDeltaImport(HttpServletRequest request)throws Exception{
		OutMessage out = new OutMessage();
		Audit audit = new Audit("solr数据增量导入", AuditConfig.DATA_DELTA_IMPORT, request);
		try {
			SolrManage.dataIncrementImport();
			out.setState(OutMessageState.Success);
			out.setContent("增量导入成功");
			audit.setFlag(AuditConfig.OP_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			out.setState(OutMessageState.Fail);
			out.setContent("增量导入失败");
			audit.setFlag(AuditConfig.OP_FAILURE);
		}
		auditService.saveAuditLog(audit);
		return JSON.toJSONString(out);
	}
	/**
	 * 检查solr服务
	 * @return
	 */
	@RequestMapping(value="/checkServer", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String checkServer(){
		OutMessage out = new OutMessage();
		try {
			
			
			//TODO 
			
			
			
			
			out.setState(OutMessageState.Success);
			out.setContent("成功");
		} catch (Exception e) {
			// TODO: handle exception
			out.setState(OutMessageState.Fail);
			out.setContent("失败");
		}
		return JSON.toJSONString(out);
	}
}
